﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.17
 * 描 述：支出事项
 */
var refreshGirdData; // 更新数据
var selectedRow;
var bootstrap = function ($, learun) {
    "use strict";
    var page = {
        init: function () {
            page.initGrid();
            page.bind();
        },
        bind: function () {
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({ keyword: keyword });
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 新增
            $('#lr_add').on('click', function () {
                selectedRow = null;
                learun.layerForm({
                    id: 'Form',
                    title: '添加支出事项',
                    url: config.webportal_server + 'pages/base/payitem/form.html?access_token=' + config.getToken().access_token,
                    width: 450,
                    height: 400,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'Form',
                        title: '编辑支出事项',
                        url: config.webportal_server + 'pages/base/payitem/form.html?access_token=' + config.getToken().access_token,
                        width: 450,
                        height: 400,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('该动作执行后此项将彻底被删除，是否确认删除该项?', function (res) {
                        if (res) {
                            learun.deleteForm(config.base_server() + 'basepayitems/delete/' + keyValue + '?access_token=' + config.getToken().access_token, {keyValue: keyValue}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            /*分类管理*/
            $('#lr_category').on('click', function () {
                learun.layerForm({
                    id: 'ClassifyIndex',
                    title: '分类管理',
                    url: config.webportal_server + 'pages/system/dict/detailIndex.html?access_token=' + config.getToken().access_token + '&itemCode=PayItemCategorys',
                    width: 800,
                    height: 500,
                    maxmin: true,
                    btn: null,
                    end: function () {
                        learun.clientdata.update('dataItem');
                        // location.reload();
                    }
                });
            });

            // 数据导出
            $('#lr_export').on('click', function () {
                location.href = config.base_server() + 'basepayitems/export?access_token=' + config.getToken().access_token;
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'basepayitems/list?access_token=' + config.getToken().access_token,
                headData: [
                    { label: "编号", name: "code", width: 120, align: "left" },
                    { label: "名称", name: "name", width: 200, align: "left" },
                    {
                        label: "支出分类", name: "category", width: 120, align: "center",
                        formatterAsync: function (callback, value, row) {
                            learun.clientdata.getAsync('dataItem', {
                                key: value,
                                code: 'PayItemCategorys',
                                callback: function (_data) {
                                    callback(_data.text);
                                }
                            });
                        }
                    },
                    {
                        label: "有效", name: "status", width: 50, align: "center",
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                        }
                    },
                    {
                      label: "更新时间", name: "updateTime", width: 160, align: "center",
                      formatter: function (value) {
                        return learun.formatDate(value, 'yyyy-MM-dd hh:mm:ss');
                      }
                    },
                    { label: "备注", name: "description", width: 200, align: "left" },
                ],
                dblclick: function (row) {
                    selectedRow = $('#gridtable').jfGridGet('rowdata');
                    var keyValue = row;
                    if (learun.checkrow(keyValue)) {
                        learun.layerForm({
                            id: 'Form',
                            title: '编辑支出事项',
                            url: config.webportal_server + 'pages/base/payitem/form.html?access_token=' + config.getToken().access_token,
                            width: 450,
                            height: 400,
                            callBack: function (id) {
                                return top[id].acceptClick(refreshGirdData);
                            }
                        });
                    }
                },
                isPage: true,
//                isTree: true,
                mainId: 'id',
                parentId: 'parentId'
            });
            page.search();
        },
        search: function (param) {
            $('#gridtable').jfGridSet('reload', param);
        }
    };

    // 保存数据后回调刷新
    refreshGirdData = function () {
        page.search();
    }

    page.init();
}


